<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Welcome extends CI_Controller {

    public function __construct()
    {
        parent::__construct();
        $this->load->helper('url_helper');
        $this->load->helper('gd_helper');
        $this->load->library('session');
        $this->load->model('Article_model');
        $this->load->model('Userinfo_model');
  		$this->load->model('Smscenter_model');
  		$this->load->model('Privatenumber_model');
    }

	public function index()
	{
		$data['image'] ='https://res.jzjgds.com/code.jpg';
		
		$this->load->view('welcome_message',$data);
	}	
	public function login(){
		$this->load->view('login',$data);
	}
	public function addfriend(){
		$this->load->view('addfriend',$data);
	}
	public function addressbook(){
		$this->load->view('addressbook',$data);
	}
	public function chatlist(){
		$this->load->view('chatlist',$data);
	}
	public function creategroup(){
		$this->load->view('creategroup',$data);
	}
	public function frienddetail(){
		$this->load->view('frienddetail',$data);
	}
	public function friendSet(){
		$this->load->view('friendSet',$data);
	}
	public function groupbook(){
		$this->load->view('groupbook',$data);
	}
	public function groupSet(){
		$this->load->view('groupSet',$data);
	}
	public function me(){
		$this->load->view('me',$data);
	}
	public function newfriendask(){
		$this->load->view('newfriendask',$data);
	}
	public function testvue(){
		$this->load->view('testvue',$data);
	}

	

	public function chatpage(){
		
		$url = "$protocol$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";        
        $timestamp = time();        
        $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        $nonceStr = "";
        for ($i = 0; $i < 16; $i++) {
            $nonceStr .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
        }        
        // 这里参数的顺序要按照 key 值 ASCII 码升序排序
        $string = "jsapi_ticket=" . getJsApiTicket();
        $string = $string . "&noncestr=" . $nonceStr;
        $string = $string . "&timestamp=" . $timestamp;
        $string = $string . "&url=https://" . $url;        
        $signature = sha1($string);        
        $signPackage = array(
            "appId"     => APPID,
            "nonceStr"  => $nonceStr,
            "timestamp" => $timestamp,
            "url"       => $url,
            "signature" => $signature,
            "rawString" => $string
        );
        $data['signPackage'] =$signPackage;
		$this->load->view('chatpage',$data);
	}




	public function demo(){
		$user=$this->session->userdata['user'];
		$data['logoImg'] = $user['logoImg'];
		$data['hy_hb_bg'] = SITEPATH."statics/wechat/images/hy_hb_bg.jpg";
        $data['zj_hb_bg'] = SITEPATH."statics/wechat/images/zj_hb_bg.jpg";
        $data['ewm_icos'] = SITEPATH."statics/wechat/images/ewm_icos.jpg";
		$this->load->view('demo',$data);
	}
	 /**
      * 获取推流地址
      * 如果不传key和过期时间，将返回不含防盗链的url
      * @param domain 您用来推流的域名
      *        streamName 您用来区别不同推流地址的唯一流名称
      *        key 安全密钥
      *        time 过期时间 sample 2016-11-12 12:00:00
      * @return String url
	 */
	function getPushUrl($domain, $streamName, $key = null, $time = null){
	      if($key && $time){
	            $txTime = strtoupper(base_convert(strtotime($time),10,16));
	            //txSecret = MD5( KEY + streamName + txTime )
	            $txSecret = md5($key.$streamName.$txTime);
	            $ext_str = "?".http_build_query(array(
	                  "txSecret"=> $txSecret,
	                  "txTime"=> $txTime
	            ));
	      }
	      return "rtmp://".$domain."/live/".$streamName . (isset($ext_str) ? $ext_str : "");
	}	
			                    
	public function getAccessToken(){
		$accessToken=getAccessToken();
		echo json_encode(array('token'=>$accessToken));
	}
	public function test(){
		$type = $this->uri->segment(3,null);
		$type2 = $this->uri->segment(4,null);
		echo $type;
		echo '<br>';
		echo $type2;
		echo '<br>';
		exit;
		
		$param = array(
			'deleted' => 0
		);
		$articles = $this->Article_model->getAll($param);
		$data = array(
			'name' => 'li',
			'articles' => $articles
		);
		echo json_encode($data);
		//$this->load->view('test/test',$data);
	}


	public function checkphone(){
		date_default_timezone_set('PRC');
		$response=$this->Smscenter_model->checkphone('17059947158');
		$result =json_decode($response,true);		
		print_r($result);		
	}
	public function releasephone(){		
		$response=$this->Smscenter_model->releasephone('17059947158','');
		$result =json_decode($response,true);
		print_r($result);
	}	


	// public function checkephone(){
	// 	$bindres=$this->Smscenter_model->bindePhone('17059946927','17358322445');
	// 	$results =json_decode($bindres,true);
	// 	print_r($results);
	// }
	// public function releaseephone(){
	// 	$bindres=$this->Smscenter_model->releaseephone('17059946927');
	// 	$results =json_decode($bindres,true);
	// 	print_r($results);
	// }

	// public function findephone(){
	// 	$bindres=$this->Smscenter_model->findephone('17059946927');
	// 	$results =json_decode($bindres,true);
	// 	print_r($results);
	// }

	public function response(){
		date_default_timezone_set("UTC");
		$postXml = $GLOBALS["HTTP_RAW_POST_DATA"]; //接收微信参数
		if($postXml){
			$this->onCallEvent($postXml);
		}		
	}
	/**
	 * 呼叫事件通知
	 * @desc 详细内容以接口文档为准
	 * @param jsonArr
	 */
	function onCallEvent($jsonBody) {
	    $jsonArr = json_decode($jsonBody, true); //将通知消息解析为关联数组
	    $eventType = $jsonArr['eventType']; //通知事件类型

	    if (strcasecmp($eventType, 'fee') == 0) {
	        file_put_contents('phonerespnse.txt','EventType error: ' . $eventType.PHP_EOL,FILE_APPEND);
	        return;
	    }

	    if (!array_key_exists('statusInfo', $jsonArr)) {
	       
	        file_put_contents('phonerespnse.txt','param error: no statusInfo.' .PHP_EOL,FILE_APPEND);
	        return;
	    }
	    $statusInfo = $jsonArr['statusInfo']; //呼叫状态事件信息

	  
	    file_put_contents('phonerespnse.txt',var_export($statusInfo,true).PHP_EOL,FILE_APPEND);
	    //callin：呼入事件
	    if (strcasecmp($eventType, 'callin') == 0) {
	        /**
	         * Example: 此处以解析sessionId为例,请按需解析所需参数并自行实现相关处理
	         *
	         * 'timestamp': 呼叫事件发生时隐私保护通话平台的UNIX时间戳
	         * 'sessionId': 通话链路的标识ID
	         * 'caller': 主叫号码
	         * 'called': 被叫号码
	         * 'subscriptionId': 绑定关系ID
	         */
	        if (array_key_exists('sessionId', $statusInfo)) {	           
	        }
	        return;
	        ;
	    }
	    //callout：呼出事件
	    if (strcasecmp($eventType, 'callout') == 0) {
	        /**
	         * Example: 此处以解析sessionId为例,请按需解析所需参数并自行实现相关处理
	         *
	         * 'timestamp': 呼叫事件发生时隐私保护通话平台的UNIX时间戳
	         * 'sessionId': 通话链路的标识ID
	         * 'caller': 主叫号码
	         * 'called': 被叫号码
	         * 'subscriptionId': 绑定关系ID
	         */
	        if (array_key_exists('sessionId', $statusInfo)) {	            
	        }
	        return;
	    }
	    //alerting：振铃事件
	    if (strcasecmp($eventType, 'alerting') == 0) {
	        /**
	         * Example: 此处以解析sessionId为例,请按需解析所需参数并自行实现相关处理
	         *
	         * 'timestamp': 呼叫事件发生时隐私保护通话平台的UNIX时间戳
	         * 'sessionId': 通话链路的标识ID
	         * 'caller': 主叫号码
	         * 'called': 被叫号码
	         * 'subscriptionId': 绑定关系ID
	         */
	        if (array_key_exists('sessionId', $statusInfo)) {
	            
	        }
	        return;
	    }
	    //answer：应答事件
	    if (strcasecmp($eventType, 'answer') == 0) {
	        /**
	         * Example: 此处以解析sessionId为例,请按需解析所需参数并自行实现相关处理
	         *
	         * 'timestamp': 呼叫事件发生时隐私保护通话平台的UNIX时间戳
	         * 'sessionId': 通话链路的标识ID
	         * 'caller': 主叫号码
	         * 'called': 被叫号码
	         * 'subscriptionId': 绑定关系ID
	         */
	        if (array_key_exists('sessionId', $statusInfo)) {
	            
	        }
	        return;
	    }
	    //disconnect：挂机事件
	    if (strcasecmp($eventType, 'disconnect') == 0) {
	        /**
	         * Example: 此处以解析sessionId为例,请按需解析所需参数并自行实现相关处理
	         *
	         * 'timestamp': 呼叫事件发生时隐私保护通话平台的UNIX时间戳
	         * 'sessionId': 通话链路的标识ID
	         * 'caller': 主叫号码
	         * 'called': 被叫号码
	         * 'stateCode': 通话挂机的原因值
	         * 'stateDesc': 通话挂机的原因值的描述
	         * 'subscriptionId': 绑定关系ID
	         */
	        if (array_key_exists('sessionId', $statusInfo)) {
	        	$bindres=$this->Smscenter_model->releasephone('17059947158','');
				$results =json_decode($bindres,true);
				file_put_contents('xuniphone.txt','jiebang: '.var_export($results,true).PHP_EOL,FILE_APPEND);
	        }
	        return;
	    }
	}

}
